<?php
/**
 * 定义获取试卷数据相关的API
 *
 * @subpackage Paper
 */

/**
 * 获取ztk的真题卷
 * @param int $id 试卷id
 * @return mixed
 */
function getZtkPaper($id) {
    global $cmsdb;
    
    $cacheKey = 'paper:ztk' . $id;
    $paperInfo = cacheGet($cacheKey);
    if ($paperInfo !== null) return $paperInfo;
    
    $paperInfo = $cmsdb->getRow("SELECT * FROM {$cmsdb->oZtkpapers} WHERE id=:paper_id",
                        array('paper_id' => $id));
    cacheAdd($cacheKey, $paperInfo, 3600);

    return $paperInfo;
}


/**
 * 获取试卷题目
 * @param int $paperId 试卷id
 * @return mixed
 */
function getZtkQuestions($paperId) {
    global $cmsdb;
    
    $cacheKey = 'paper:ztk:Q:' . $paperId;
    $questions = cacheGet($cacheKey);
    if ($questions !== null) return $questions;

    $sql = "SELECT q.id, q.category FROM `{$cmsdb->oZtkQuestions}` AS q JOIN `$cmsdb->paperQuestionR` AS r "
        . ' ON r.question_id=q.id AND r.paper_id=:paper_id AND q.type=1 order by r.number ASC, r.id ASC';
    $results = $cmsdb->getResults($sql, array('paper_id' => $paperId));
    cacheAdd($cacheKey, $results, 3600);

    return $results;
}
?>